<?php
/**
 * 人力资源
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class ResourcesController extends CommonController{
	public function way(){
		session("action","Resources");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}
	
	public function index(){
		$this->redirect("lists");
	}
	
	Public function lists(){
		$model = M();
		$personnel = M("personnel_files");
		$name = I('post.name');
		$sql = "select personnel.*,dept.dept_name,priv.priv_name as count from oa_personnel_files personnel,oa_sys_dept dept,oa_sys_priv priv where personnel.dept_id = dept.id and personnel.priv_id = priv.id";
		if($name != ''){$sql .= " and name = '$name'";}
		$res = $model->query($sql);
		$count = count($res);
		$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
		$show = $page->show();
		
		$sql .=  " limit ".$page->firstRow.",".$page->listRows;
		$data = $model->query($sql);
		$this->assign('data',$data);
		$this->assign("page",$show);
		$this->assign("empty",emptyhtml("list"));
		$this->assign("name",$name);
		$this->display();
	}
	
	public function add(){
		$dept = M("sys_dept");
		$priv = M("sys_priv");
		$data_dept = $dept->order('dept_no')->select();
		$data_priv = $priv->order('priv_no')->select();
		$this->assign('data_dept',$data_dept);
		$this->assign('data_priv',$data_priv);
		$this->display();
	}
	
	public function insert(){
		if(!IS_POST){ $this->error("非法表单"); }
		$model = M("personnel_files");
		$name = trim(I('post.name'));
		if( empty( $name ) ){$this->error("姓名不能为空");}
		$data = I('post.');
		$success = $model->data($data)->add();
		if($success){
			$this->systemlog("add", "人员档案", "添加人员信息---".$name);
			$this->success("添加人员信息成功！", 'lists');
		}else{
			$this->error("添加人员信息失败！");
		}
	}
	
	public function edit(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$model = M("personnel_files");
		$data = $model->where("id=%d",$id)->find();
		
		$dept = M("sys_dept");
		$priv = M("sys_priv");
		$data_dept = $dept->order('dept_no')->select();
		$data_priv = $priv->order('priv_no')->select();
		
		$this->assign("data_personnel", $data);
		$this->assign('data_dept',$data_dept);
		$this->assign('data_priv',$data_priv);
		$this->display();
	}
	
	public function save(){
		$id = I('post.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		if(!IS_POST){ $this->error("非法表单"); }
		
		$model = M("personnel_files");
		$name = trim(I('post.name'));
		if( empty( $name ) ){$this->error("姓名不能为空");}
		$data = I('post.');
		$success = $model->where("id=%d",$id)->data($data)->save();
		if($success){
			$this->systemlog("edit", "人员档案", "修改人员信息---".$name);
			$this->success("修改人员信息成功！", 'lists');
		}else{
			$this->error("修改人员信息失败！");
		}
	}
	
	public function delete(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$model = M("personnel_files");
		$name = $model->where("id=%d",$id)->getField("name");
		$success = $model->where("id=%d",$id)->delete();
		if($success){
			$this->systemlog("delete", "人员档案", "删除人员信息---".$name);
		}
	}
	
	public function view(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$personnel = M("personnel_files");
		$dept = M("sys_dept");
		$priv = M("sys_priv");
		$data_personnel = $personnel->where('id=%d', $id)->find();
		$data_dept = $dept->order('dept_no')->select();
		$data_priv = $priv->order('priv_no')->select();
		$this->assign('data_personnel',$data_personnel);
		$this->assign('data_dept',$data_dept);
		$this->assign('data_priv',$data_priv);
		$this->display();
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>